package com.lucsa.utils;

import com.lucsa.jsonUtils.Serializer;
import com.lucsa.jsonUtils.jsonUtil;
import com.lucsa.pojo.RpcRequest;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.MessageToMessageDecoder;

import java.util.List;

public class RpcDecoder extends MessageToMessageDecoder<ByteBuf> {
    private Serializer serializer;
    private Class<?> clzz;

    public RpcDecoder(Class<?> clzz,Serializer serializer){
        this.serializer = serializer;
        this.clzz = clzz;
    }

    @Override
    protected void decode(ChannelHandlerContext ctx, ByteBuf msg, List<Object> out) throws Exception {

        byte[] bytes = new byte[msg.readableBytes()];
        msg.readBytes(bytes);
        RpcRequest deserialize = (RpcRequest) serializer.deserialize(this.clzz,bytes);
        out.add(deserialize);
    }
}
